﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using personalplaner.business.enums;
using personalplaner.business.stamm;
using personalplaner.common.mvvm;
using personalplaner.common.mvvm.commands;
using personalplaner.gui.stamm.viewmodels.models;
using personalplaner.gui.stamm.views;
using personalplaner.servicemodel.dataaccess;
using personalplaner.gui.common;
using personalplaner.business;

namespace personalplaner.gui.stamm.viewmodels
{
	public class SetPersonPensumViewModel : BaseViewModel
	{

		public SetPersonPensumViewModel(DependencyObject parent)
			: base(parent)
		{
		}

		public override void Loaded()
		{
			var mw = GetService<IMainWindowDisplayContainer>();
			mw.IsWaitVisible = false;
		}

        #region Commands

        ICommand _okcommand;
        public ICommand OkCommand
        {
            get
            {
                if (_okcommand == null)
                    _okcommand = new RelayCommand(OnOkCommand);
                return _okcommand;
            }
        }

        private void OnOkCommand(object parameter)
        {
            var wnd = Parent as Window;
            if (wnd != null)
            {
                wnd.DialogResult = true;
                wnd.Close();
                return;
            }

            var mw = GetService<IMainWindowDisplayContainer>();
            mw.MoveToPreviousView();
        }

        ICommand _cancelcommand;
        public ICommand CancelCommand
        {
            get
            {
                if (_cancelcommand == null)
                    _cancelcommand = new RelayCommand(OnCancelCommand);
                return _cancelcommand;
            }
        }

        private void OnCancelCommand(object parameter)
        {
            var wnd = Parent as Window;
            if (wnd != null)
            {
                wnd.DialogResult = false;
                wnd.Close();
                return;
            }

            var mw = GetService<IMainWindowDisplayContainer>();
            mw.MoveToPreviousView();
        }

        //private ICommand _viewhistorycommand;
        //public ICommand ViewHistoryCommand
        //{
        //    get
        //    {
        //        if (_viewhistorycommand == null)
        //            _viewhistorycommand = new RelayCommand(OnViewHistory);
        //        return _viewhistorycommand;
        //    }
        //}

        //private void OnViewHistory(object parameter)
        //{
        //    //CWA: implement the history viewer
        //}

        private ICommand _addPensumCommand;
        public ICommand AddPensumCommand
        {
            get
            {
                if (_addPensumCommand == null)
                    _addPensumCommand = new RelayCommand(OnAddPensum);
                return _addPensumCommand;
            }
        }

        private void OnAddPensum(object parameter)
        {
            if (Model.Prozent <= 0)
            {
                MessageBox.Show(Window.GetWindow(Parent), Properties.Resources.EnterPensum, Properties.Resources.NoPensum, MessageBoxButton.OK, MessageBoxImage.Asterisk);
                return;
            }

            if (MessageBox.Show(Window.GetWindow(Parent), Properties.Resources.PensumChangeQuestion, Properties.Resources.ChangePensum,
                MessageBoxButton.OKCancel, MessageBoxImage.Warning) != MessageBoxResult.OK)
                return;

            if (SaveData())
            {
                if (!Model.IsKuendigung)
                {
                    //Pensum ist pro Abteilung
                    PensumChanged = true;
                }

                // reset/refresh the data
                var da = GetService<IPensumDataAccess>();
                ArbeitsPensen = new ObservableCollection<Pensum>(da.GetPensen(PrsID));

                var pens = da.GetPensum(PrsID, DateTime.Now);
                if (pens == null)
                    pens = new Pensum();

                Model.Von = DateTime.Now;
                Model.Bis = DateTime.Now.AddYears(20);

                Model.Prozent = pens.Prozent;

                if (MessageBox.Show(Properties.Resources.PensumChangedEditAbteilungePensum, Properties.Resources.Pensum, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    var service = GetService<IMainWindowDisplayContainer>();
                    service.AddSimpleContentItem(enDisplayItemType.SetPersonAbteilung, new SetPersonAbteilung(PrsID), true);
                }
            }
            else
                MessageBox.Show(Properties.Resources.ErrorSavingData, Properties.Resources.Error, MessageBoxButton.OK);
        }

        private ICommand _deletePensumCommand;
        public ICommand DeletePensumCommand
        {
            get
            {
                if (_deletePensumCommand == null)
                    _deletePensumCommand = new RelayCommand<Pensum>(OnDeletePensum);
                return _deletePensumCommand;
            }
        }

        private void OnDeletePensum(Pensum parameter)
        {
            if (parameter == null)
                return;

            if (MessageBox.Show(string.Format(Properties.Resources.PensumEntfernenQuestionFormat, parameter.Prozent, parameter.Von.ToShortDateString(), parameter.Bis.ToShortDateString()), Properties.Resources.PensumEntfernen, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                return;

            var da = GetService<IPensumDataAccess>();
            if (da.DelPensum(parameter))
            {
                ArbeitsPensen.Remove(parameter);
            }
        }

        #endregion

        private bool SaveData()
        {
            //bool retval = true;

            if (Model.Prozent > 100 || Model.Prozent <= 0)
                return false;

            if (Model.Von > Model.Bis && !Model.IsKuendigung)
                return false;

            var da = GetService<IPensumDataAccess>();
            if (Model.IsKuendigung)
                return da.SetKuendigung(PrsID, Model.Bis);

            return da.SetPensum(PrsID, Model.Prozent, Model.Von, Model.Bis);
        }

        public override void Unload()
        {
            // use mediator to notify all suscribed methods that a person has to be updated
            Mediator.NotifyColleagues<int>("UpdatePersonPensum", PrsID);
        }
        
        #region Properties

        private int _prsID;
		public int PrsID
		{
			get { return _prsID; }
			set
			{
				_prsID = value;
				RaisePropertyChanged("PrsID");

				var da = GetService<IPensumDataAccess>();
                var lst = da.GetPensen(PrsID);
                ArbeitsPensen = lst != null ? new ObservableCollection<Pensum>(lst) : new ObservableCollection<Pensum>();

                Model = new SetPersonPensumModel();
                Model.Von = DateTime.Now;
                Model.Bis = DateTime.Now.AddYears(20);

                var pens = da.GetPensum(PrsID, DateTime.Now);
                if (pens == null)
                    return;

                Model.Prozent = pens.Prozent;

			}
		}


		private SetPersonPensumModel _model;
		public SetPersonPensumModel Model
		{
			get
			{
				return _model;
			}
			set
			{
				_model = value;
				RaisePropertyChanged("Model");
			}
		}

		private ObservableCollection<Pensum> _arbeitsPensen;
		public ObservableCollection<Pensum> ArbeitsPensen
		{
			get
			{
				return _arbeitsPensen;
			}
			set
			{
				_arbeitsPensen = value;
				RaisePropertyChanged("ArbeitsPensen");
			}
		}


		private Pensum _activeGridItem;
		public Pensum ActiveGridItem
		{
			get { return _activeGridItem; }
			set
			{
				_activeGridItem = value;
				RaisePropertyChanged("ActiveGridItem");
			}
        }

        public bool PensumChanged { get; private set; }

        #endregion
    }
}
